package com.srwl.mytx.converter;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.parser.ParserConfig;

import java.io.IOException;
import java.lang.reflect.Type;

import okhttp3.ResponseBody;
import retrofit2.Converter;

final class MyResponseBodyNotDecodeConverter<T> implements Converter<ResponseBody, T> {
    private String TAG = "MyResponseBodyNotDecodeConverter";
    private static final Feature[] EMPTY_SERIALIZER_FEATURES = new Feature[0];

    private Type mType;
    private ParserConfig config;
    private int featureValues;
    private Feature[] features;

    MyResponseBodyNotDecodeConverter(Type type, ParserConfig config, int featureValues,
                                     Feature... features) {
        mType = type;
        this.config = config;
        this.featureValues = featureValues;
        this.features = features;
    }

    @Override
    public T convert(ResponseBody value) throws IOException {
        String encrypt = new String(value.bytes());
        JSONObject jsonObject = JSONObject.parseObject(encrypt);

        Log.d(TAG, "返回数据未加密：" + encrypt);
        return JSON.parseObject(jsonObject.toJSONString(), mType, config, featureValues,
                features != null ? features : EMPTY_SERIALIZER_FEATURES);

    }
}
